//
// Created by Semir on 2020/5/7.
//

#include <iostream>
#include <vector>

using namespace std;

bool Find(int target, vector<vector<int>> array) {
    if (array.empty()) {
        return false;
    }
    int i = 0;
    int j = array[0].size() - 1;
    while (i < array.size() && j >= 0) {
        if (array[i][j] != target) {
            if (array[i][j] > target) {
                j--;
            } else {
                i++;
            }
        } else {
            return true;
        }
    }
    return false;
}

void Test() {
    vector<vector<int>> array(5, vector<int>(5));
    array[0] = {1,   4,  7, 11, 15};
    array[1] = {2,   5,  8, 12, 19};
    array[2] = {3,   6,  9, 16, 22};
    array[3] = {10, 13, 14, 17, 24};
    array[4] = {18, 21, 23, 26, 30};
    bool res1 = Find(5, array);
    bool res2 = Find(20, array);
    cout << res1 << endl;
    cout << res2 << endl;
}

int main() {
    Test();
    return 0;
}
